﻿using System.Collections.Generic;
using System.Windows.Controls;
using PSE.Deployment.SetupBase.Configuration;

namespace PSE.Deployment.ConfigurationManager.Controls
{
	/// <summary>
	/// UserControl que instancia um botao de remoção de tag
	/// </summary>
	public class RemoveTagButton : Button
	{
		SimpleGroupBoxes _parentItem;
		SimpleGroupBox _item;
		List<XmlTag> _tags = null;
		XmlTag _tag = null;

		public RemoveTagButton(List<XmlTag> tags, XmlTag tag, SimpleGroupBoxes parentItem, SimpleGroupBox item)
		{
			_tags = tags;
			_tag = tag;
			_parentItem = parentItem;
			_item = item;

			this.Content = "Excluir (-)";
			this.SetResourceReference(AddTagButton.StyleProperty, "ButtonCommon");
			this.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
			this.VerticalAlignment = System.Windows.VerticalAlignment.Top;

			this.Click += new System.Windows.RoutedEventHandler(TagRemoveButton_Click);
		}

		/// <summary>
		/// Tratamento do evento click do controle
		/// </summary>
		void TagRemoveButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            if (_tags != null && _tag != null && _tags.Contains(_tag))
            {
                _tags.Remove(_tag);
                
                foreach (XmlTag tag in _tags)
                {
                    tag.HasChanges = true;
                }
            }

			if (_parentItem.Children.Contains(_item))
				_parentItem.Children.Remove(_item);

			_parentItem.ForbidAllowLastItemRemoval();
		}
	}
}